package cn.stylefeng.guns.modular.dms.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.dms.entity.Directory;
import cn.stylefeng.guns.modular.dms.model.params.DirectoryParam;
import cn.stylefeng.guns.modular.dms.model.result.DirectoryResult;
import cn.stylefeng.guns.modular.dms.service.DirectoryService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


/**
 * 文件目录控制器
 *
 * @author yzb
 * @Date 2019-11-09 09:11:36
 */
@Controller
@RequestMapping("/directory")
public class DirectoryController extends BaseController {

    private String PREFIX = "/dms/directory";

    @Autowired
    private DirectoryService directoryService;

    /**
     * 跳转到主页面
     *
     * @author yzb
     * @Date 2019-11-09
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/directory.html";
    }

    /**
     * 新增页面
     *
     * @author yzb
     * @Date 2019-11-09
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/directory_add.html";
    }

    /**
     * 编辑页面
     *
     * @author yzb
     * @Date 2019-11-09
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/directory_edit.html";
    }

    /**
     * 新增接口
     *
     * @author yzb
     * @Date 2019-11-09
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(DirectoryParam directoryParam) {
        this.directoryService.add(directoryParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author yzb
     * @Date 2019-11-09
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(DirectoryParam directoryParam) {
        this.directoryService.update(directoryParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author yzb
     * @Date 2019-11-09
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(DirectoryParam directoryParam) {
        this.directoryService.delete(directoryParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author yzb
     * @Date 2019-11-09
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(DirectoryParam directoryParam) {
        Directory detail = this.directoryService.getById(directoryParam.getDirectoryId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author yzb
     * @Date 2019-11-09
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(DirectoryParam directoryParam) {
        return this.directoryService.findPageBySpec(directoryParam);
    }

    /**
     * 查询列表
     *
     * @author yzb
     * @Date 2019-11-09
     */
    @ResponseBody
    @RequestMapping("/listAll")
    public ResponseData listAll(DirectoryParam directoryParam) {
        List<DirectoryResult> results=this.directoryService.findListBySpec(directoryParam);
        return ResponseData.success(results);
    }


    /**
     * 查询列表
     *
     * @author yzb
     * @Date 2019-11-09
     */
    @ResponseBody
    @RequestMapping("/listAllForChild")
    public ResponseData listAllForChild(DirectoryParam directoryParam) {
        //全部目录
        List<DirectoryResult> results=this.directoryService.findListBySpec(directoryParam);
        //一级目录（包含子目录）
        List<DirectoryResult>  directoryResults=new ArrayList<>();
        for (DirectoryResult directoryResult:results){
            if (directoryResult.getParentId()==0){
                directoryResults.add(directoryResult);
            }
        }
        //子目录
        List<DirectoryResult> childs=null;
        for (int i=0;i< directoryResults.size();i++){
            childs=new ArrayList<>();
            for (DirectoryResult result1:results){
                if (directoryResults.get(i).getDirectoryId()==result1.getParentId()){
                    childs.add(result1);
                }
            }
            directoryResults.get(i).setChild(childs);
        }
        return ResponseData.success(directoryResults);
    }

}


